package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.www.car.actionform.CarCreationInputActionForm;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang  huangwei st2
 * 立体停车场入库中间跳转action
 */
public class CarCreationEasyDispatchAction extends TmsAction{
	private Logger logger = Logger.getLogger(CarCreationEasyDispatchAction.class);
	private static final String VALIDATElICENSEPLATE = "validateLicensePlate";
	private static final String TORESULT = "toresult";
	private static final String TOEASYINPUT = "toEasyInput";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String forward = "";
		CarCreationInputActionForm inputForm = (CarCreationInputActionForm)form; 
		CarCreationInputViewHelper viewhelper = new CarCreationInputViewHelper();
		viewhelper = (CarCreationInputViewHelper)uc.getAttribute(VIEWHELP);
		
		viewhelper.setQRPrintFlg(inputForm.getQRPrintFlg());

		//
		if(!StringUtil.isNullOrBlank(inputForm.getCarStyleId())){
			CarStyle carStyle = CarStyle.getInstance();
			String styleName = "";
			String bb = "";
			if(inputForm.getCarStyleId().startsWith("S")){
				CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
				styleName = commercialVehicle.getCarStyleRNameById(inputForm.getCarStyleId());
				String dis[] = styleName.split("-");		
				 bb = dis[1];
			}else{
				styleName = carStyle.getCarStyleRNameById(inputForm.getCarStyleId());
				bb = styleName;
			}
			viewhelper.setDisplacementS(bb);
		}
		viewhelper.setColor(String.valueOf(inputForm.getColor()));
		viewhelper.setNote(inputForm.getNote());
		viewhelper.setLcPrice(inputForm.getLcPrice());
		String lPlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);//车牌前两位
//		if(inputForm.getLicensePlate().equals(lPlate)) viewhelper.setLicensePlate("");
//		else  viewhelper.setLicensePlate(inputForm.getLicensePlate());
		viewhelper.setLicensePlate(inputForm.getLicensePlate());
//		viewhelper.setDisplacement(String.valueOf(inputForm.getDisplacement()));
//		viewhelper.setGearbox(String.valueOf(inputForm.getGearbox()));
		viewhelper.setFromType(inputForm.getFromType());
		if(viewhelper.getCarNameVisibleFlag() == 1){
			viewhelper.setVendorId(viewhelper.getVendorId());
			viewhelper.setBrandId(viewhelper.getBrandId());
			viewhelper.setCarStyleId(viewhelper.getCarStyleId());
			viewhelper.setCarStyle(viewhelper.getCarStyleId());
		}else {
			viewhelper.setVendorId(inputForm.getVendorId());
			viewhelper.setBrandId(inputForm.getBrandId());
			viewhelper.setCarStyleId(inputForm.getCarStyleId());
			viewhelper.setCarStyle(inputForm.getCarStyleId());
		}
		viewhelper.setRegDate(inputForm.getRegDate());
//		viewhelper.setSendCardDate(inputForm.getSendCardDate());
//		viewhelper.setAttestStatus(inputForm.getAttestStatus());
		viewhelper.setPrice(String.valueOf(inputForm.getPrice()));
		viewhelper.setMileage(String.valueOf(inputForm.getMileage()));		

		viewhelper.setCarOwner(inputForm.getCarOwner());
		viewhelper.setCardType(inputForm.getCardType());
		viewhelper.setBackFlag(0);
		viewhelper.setJqx(inputForm.getJqx());
		Member member = Member.getInstance();
		Codes code = Codes.getInstance();
		viewhelper.setMemberList(member.findAllMemInfo());// 会员类型
		viewhelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		viewhelper.setChestr(inputForm.getChestr());
		viewhelper.setMemId(inputForm.getMemId());
		viewhelper.setMemType(inputForm.getMemType());
		viewhelper.setSmjf(inputForm.getSmjf());
		viewhelper.setYearCarefulPeriod(inputForm.getYearCarefulPeriod());
		viewhelper.setCompanyContactPerson(inputForm.getCompanyContactPerson());
		viewhelper.setCompanyContactPhone(inputForm.getCompanyContactPhone());
		viewhelper.setVin(inputForm.getVin());
		viewhelper.setTempLicensePlate(inputForm.getTempLicensePlate());
		viewhelper.setRemark(inputForm.getRemark());
		viewhelper.setPhoto_1(inputForm.getImg1());
		viewhelper.setPhoto_2(inputForm.getImg2());
		viewhelper.setPhoto_3(inputForm.getImg3());
		viewhelper.setPhoto_4(inputForm.getImg4());
		viewhelper.setPhoto_5(inputForm.getImg5());
		viewhelper.setPhoto_6(inputForm.getImg6());
		viewhelper.setPhoto_7(inputForm.getImg7());
		viewhelper.setPhoto_8(inputForm.getImg8());
		viewhelper.setIsPay(inputForm.getIsPay());
		viewhelper.setIsShelve(inputForm.getIsShelve());
		viewhelper.setRegisterStatus(inputForm.getRegisterStatus());
		viewhelper.setGateType(inputForm.getGateType());
		viewhelper.setHandleInfoRemark(inputForm.getHandleInfoRemark());
		//车辆名称取值
		VehicleBrand brand = VehicleBrand.getInstance();
		String brandIdStr = "";
		if(StringUtil.isNullOrBlank(inputForm.getCarBrand())){
			brandIdStr = inputForm.getBrandId();
		}else{
			brandIdStr = inputForm.getCarBrand();
		}
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		if (StringUtil.isNullOrBlank(inputForm.getCarBrand()) && StringUtil.isNullOrBlank(inputForm.getBrandId())) {
			viewhelper.setCarName(viewhelper.getCarName());
//			viewhelper.setGearbox("");
			viewhelper.setCountryStyle("");
		} else {
			List list = new ArrayList();
			if(inputForm.getFromType() == 1){
				list = brand.getAllVehicleBrandInfo(inputForm.getVendorId());
			}else{
				list = commercialVehicle.getVehicleBrandInfo(inputForm.getVendorId());
			}
			
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);
					if (map.get("brandId").equals(brandIdStr)) {
						String carName = (String) map.get("brandName");
						viewhelper.setCarName(carName);
					}
				}
			}else{
				viewhelper.setCarName(" ");
			}
		}
		//
		if("9".equals(inputForm.getSaveInputInfo())){
			viewhelper.setSaveFlag(9);
			forward = VALIDATElICENSEPLATE;
		}else if("10".equals(inputForm.getSaveInputInfo())){
			viewhelper.setSaveFlag(10);
			forward = VALIDATElICENSEPLATE;
		}else if("11".equals(inputForm.getSaveInputInfo())){
			viewhelper.setSaveFlag(11);
			String path =ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOADTMP_CONTEXT_ROOT_KEY));
			viewhelper.setAcceptorImage(path + inputForm.getAcceptorImage());
			forward = TOEASYINPUT;
			request.setAttribute(VIEWHELP, viewhelper);	
		}else {
			forward = TORESULT;
		}
		if("4".equals(inputForm.getSaveInputInfo())){
			viewhelper.setSaveFlag(1);
		}
		if("5".equals(inputForm.getSaveInputInfo())){
			
			viewhelper.setSaveFlag(3);
		}
		if("6".equals(inputForm.getSaveInputInfo())){
			
			viewhelper.setSaveFlag(6);//流程表
			viewhelper.setTransferPrice(inputForm.getTransferPrice());
		}
		if("7".equals(inputForm.getSaveInputInfo())){
			
			viewhelper.setSaveFlag(7);//查验表
		}
		if("3".equals(inputForm.getSaveInputInfo())){
			
			viewhelper.setSaveFlag(5);
		}
        uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(forward);
	}

}
